mui.init({
	swipeBack: true
});

function showPrompt() {
	var bts = ["确认", "取消"];
	mui.prompt('设置备注', function(value) {
		var message = value.message;

	});
}
mui.plusReady(function() {
	var ws = plus.webview.currentWebview(),
		friend_id = plus.storage.getItem('temp_friend_id'),
		user_id = plus.storage.getItem('cg_user_id'),
		user_token = plus.storage.getItem('cg_user_token'),
		remark = '',
		isEnd = true, //是否还有数据可加载的标志
		isLoad = false, //ajax是否还在加载
		remark_reg = /^[a-zA-Z\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]*$/;
	ws.setBounce({ //安卓设置回弹效果
		position: {
			top: '244px'
		},
		changeoffset: {
			top: '0'
		}
	});
	myAjax('addresslist/user_profile/getProfileById', 'get', {
		'user_id': user_id,
		'user_token': user_token,
		'get_user_id': friend_id
	}, function(data) {
		if(data.success){
			info_success(data);
		}else{
			mui.back();
			plus.nativeUI.closeWaiting();
			mui.alert('操作失败，请重试！');
		}
	});

	getCards();
	//滑动加载更多
	$(window).scroll(function() {
		if($(document).height() - $(this).scrollTop() - $(this).height() < 10 && !isLoad && isEnd) {
			var update_time = $('#update_time').attr('href');
			isLoad = true;
			getCards(update_time);
		}
	});
	//帖子渲染函数
	function getCards(update_time) {
		var datas = {},
			page_cnt = 3;
		if(!update_time) {
			datas = {
				'user_id': user_id,
				'user_token': user_token,
				'get_user_id': friend_id,
				'page_cnt': page_cnt
			}
		} else {
			$('.icon-loading').show();
			datas = {
				'user_id': user_id,
				'user_token': user_token,
				'page_cnt': page_cnt,
				'get_user_id': friend_id,
				'update_time': update_time
			}
		}
		myAjax('card/card/getUserCard', 'get', datas, function(data) {
			var cards = data.data,
				html = [],
				img_text = '<div class="friend-card-item img-text white-background" id={{article_id}}>' +
				'<div class="card-img">' +
				'<img src="' + _baseUrl + '{{path}}" />' +
				'</div>' +
				'<span class="card-headline">{{title}}</span>' +
				'<span class="card-date">{{update_time}}</span>' +
				'</div>',
				only_text = '<div class="friend-card-item only-text white-background" id={{article_id}}>' +
				'<span class="card-headline">{{title}}</span>' +
				'<span class="card-date">{{update_time}}</span>' +
				'</div>';
			if(cards.length == 0) {
				$('.friend-cards').remove();
			} else if(cards.length < page_cnt) {
				$('.mui-text-center').hide();
			}
			mui.each(cards, function(key, value) {
				if(value.status == 1) {
					var _html = '';
					if(value.path) {
						_html = img_text.replace('{{article_id}}', value.article_id)
							.replace('{{path}}', value.path)
							.replace('{{title}}', value.title)
							.replace('{{update_time}}', value.update_time);
					} else {
						_html = only_text.replace('{{article_id}}', value.article_id)
							.replace('{{title}}', value.title)
							.replace('{{update_time}}', value.update_time);
					}
					html.push(_html);
				}
			});
			$('.friend-cards').append(html.join(''));
			$('#update_time').attr('href', data.update_time);
			isEnd = data.data.length == page_cnt ? true : false;
			if(isEnd) {
				$('.icon-loading').hide();
			} else {
				$('.mui-text-center').hide();
			}
		});
		isLoad = false;
	}
	$('.tel-wrap-value').on('tap', function(e) {
		e.preventDefault();
		var telephone = $(this).text();
		mui.confirm('确定拨打电话：' + telephone + '吗？', '提示', function(value) {
			if(value.index == 1) {
				if(plus.os.name == 'iOS') {
					plus.device.dial(telephone, false);
				} else {
					var Uri = plus.android.importClass("android.net.Uri");
					var Intent = plus.android.importClass("android.content.Intent");
					var number = new Uri.parse("tel:" + telephone);
					var callIntent = new Intent(Intent.ACTION_CALL, number);
					plus.android.runtimeMainActivity().startActivity(callIntent);
				}
			}
		}, ['确定', '取消']);
	});
	//进入帖子详细页面
	$('.friend-cards').delegate('.friend-card-item', 'tap', function() {
		var article_id = $(this).attr('id');
		mui.openWindow({
			url: '../../pages/city/tribune_detail.html',
			id: 'tribune_detail',
			extras: {
				'article_id': article_id
			}
		});
	});
	$('.set-remark').on('touchstart', function(e) {
		e.preventDefault();
		mui.prompt('设置备注', function(value) {
			var message = value.message;
			if(value.index == 1) {
				if(remark_reg.test(message) && message.length <= 12) {
					myAjax('addresslist/user_profile/changeSign', 'post', {
						'user_id': user_id,
						'user_token': user_token,
						'to_user_id': friend_id,
						'sign': message
					}, function(data) {
						if(data.success) {
							mui.alert(data.msg, '提示');
							$('.remark').text(message);
							refresh_address_list();
						} else {
							mui.alert('修改失败，请尝试其他名字进行备注', '提示');
						}
					});
				} else if(!remark_reg.test(message)) {
					if(message == '') {
						mui.alert('请输入备注名称', '提示');
					} else {
						mui.alert('备注必须由汉字数字字母组成，并且数字不能开头', '提示');
					}
				} else {
					mui.alert('备注长度不得大于12位', '提示');
				}
			}
		});
	});
	//刷新通讯录页面函数
	function refresh_address_list() {
		var list = plus.webview.getWebviewById('address_list.html');
		//触发列表界面的自定义事件（refresh）,从而进行数据刷新
		mui.fire(list, 'refresh');
		//返回true，继续页面关闭逻辑
		return true;
	}
	//渲染用户信息函数
	function info_success(data) {
		var friend_info = data.data,
			city = '江苏省 常州市',
			h = plus.webview.getTopWebview();
		if(friend_info.city != '') {
			var temp = friend_info.city.split('|');
			city = temp[1] + " " + temp[3];
		}
		$('.friend-headimg img').attr('src', _baseUrl + friend_info.headimgurl);
		refresh_address_list();
		$('.tel-wrap-value').text(friend_info.mobile);
		$('.area-wrap-value').text(city);
		$('.signiture-wrap-value').text(friend_info.signature);
		$('.remark').addClass(friend_info.sex == '2' ? 'female' : 'male');
		if(friend_info.is_friend == 0) {
			$('.set-remark').remove();
		}
		if(friend_info.sign == '') {
			$('.username').text(friend_info.username);
			$('.remark').text(friend_info.nickname);
		} else {
			$('.username').text(friend_info.username);
			$('.remark').text(friend_info.sign);
			$('.friend-main-info').append('<small class="nickname">' + friend_info.nickname + '</small>')
		}
		var page = plus.webview.getWebviewById('friend_info_header.html');
		mui.fire(page, 'pageflowrefresh', {
			'is_friend': friend_info.is_friend
		});
	}
});